﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Web.Services;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using Web.Models;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using Microsoft.WindowsAzure.ServiceRuntime;

namespace Web.Helper
{
    public class SaudoLogic
    {
        private CloudStorageAccount account;
        private SaudoContext context;
        private CloudBlobClient blobs;

        public SaudoLogic()
        {
            string connectionString = RoleEnvironment.GetConfigurationSettingValue("storage");
            CloudStorageAccount account = CloudStorageAccount.Parse(connectionString);

            Init(account);
        }

        public SaudoLogic(CloudStorageAccount account)
        {
            Init(account);
        }

        private void Init(CloudStorageAccount account)
        {
            this.account = account;

            CloudTableClient.CreateTablesFromModel(
                typeof(SaudoContext),
                account.TableEndpoint.AbsoluteUri,
                account.Credentials);
            context = new SaudoContext(account.TableEndpoint.AbsoluteUri, account.Credentials);
        }

        public Saudo SendSaudo(string sender, string receiver, string text)
        {
            // TODO: Validate sender and receiver (sollte email sein)
            if (string.IsNullOrEmpty(text))
                throw new ArgumentNullException("text");

            // Create
            Saudo saudo = new Saudo()
            {
                RowKey = Guid.NewGuid().ToString(),
                PartitionKey = receiver,
                Day = DateTime.UtcNow.Date,
                Sender = sender,
                Text = text
            };
            context.AddObject("Saudos", saudo);
            context.SaveChanges();

            return saudo;
        }

        public void VoteSaudo(string key)
        {
            // exists?
            Saudo saudo = context.Saudos.Where((s) => (s.RowKey == key)).FirstOrDefault();

            if (saudo == null)
                throw new Exception("Saudo does not exist");

            saudo.Votes++;
            context.UpdateObject(saudo);
            context.SaveChanges();
        }

        public void CommentSaudo(string key, string text)
        {
            // exists?
            Saudo saudo = (from s in context.Saudos
                where s.RowKey == key
                select s).FirstOrDefault();

            if (saudo == null)
                throw new Exception("Saudo does not exist");

            // TODO: Check parameter
            if (string.IsNullOrEmpty(text))
                throw new ArgumentNullException("text");

            // Comment
            context.AddObject("Comments", new Comment()
            {
                RowKey = Guid.NewGuid().ToString(),
                PartitionKey = key,
                Text = text
            });
            context.SaveChanges();
        }
    }
}